package com.aaa.hospital.ManagementEnd.controller;


import com.aaa.hospital.Client.pojo.Visitingcard;
import com.aaa.hospital.Client.service.VisitingcardService;
import com.aaa.hospital.Client.service.Visitingcard_xyService;
import com.aaa.hospital.ManagementEnd.pojo.Diagnose;
import com.aaa.hospital.ManagementEnd.pojo.Examine;
import com.aaa.hospital.ManagementEnd.pojo.ExpenseCalendar;
import com.aaa.hospital.ManagementEnd.pojo.Prescription;
import com.aaa.hospital.ManagementEnd.service.*;

import com.aaa.hospital.ManagementEnd.service.Impl.CheckSerImpl;
import com.aaa.hospital.ManagementEnd.service.Impl.MedicinImpl;
import com.aaa.hospital.ManagementEnd.util.DiagnoseDB;
import com.aaa.hospital.ManagementEnd.util.PrescriptionDB;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

//lgq
@Controller
@RequestMapping("/diagnose")
public class DiagnoseController {
    @Autowired
    private DiagnoseService diagnoseService;
    @Autowired
    private PrescriptionService prescriptionService;
//    @Autowired
//    private CheckSerImpl checkSerImpl;
    @Autowired
    private MedicinImpl medicinImpl;
    //预约
    @Autowired
    private RegistrationServices registrationServices;
    @Autowired
    private MedChargeSerevice medChargeSerevice;

    @Autowired
    private ExpenCalendService expenCalendService;

    @Autowired
    private VisitingcardService visitingcardService;
    @Autowired
    private Visitingcard_xyService xyService;
@RequestMapping("/insert")
@ResponseBody
  public boolean insert(Integer yydid,Integer ydbh,Integer visid,Integer  tjid,Integer did,String bqms,String grs,String jzs,String yszd,String yz){
    Diagnose diagnose=new Diagnose(null,ydbh,visid,did,tjid,null,bqms,yszd,jzs,grs,yz,0,null);
    boolean insert = diagnoseService.insert(diagnose);
    if (insert == true) {
        return registrationServices.update_state(yydid, 2);
    }else {
    return false;
    }
  }
//门诊收费
@RequestMapping("/Mzprice")
@ResponseBody
    public boolean price(Integer visid,Integer price,String usefor){
    boolean byvisid = medChargeSerevice.Byvisid(visid, price);//判断月是否足够支付，够，直接减去金额，反之返回false
    ExpenseCalendar ex=new ExpenseCalendar();
    ex.setVisid(visid);
    ex.setPurpose(usefor);
    ex.setMoney(price);
    if (byvisid == true) {
      return   expenCalendService.ecInsert(ex);// 添加消费记录信息
    }else {
        return false;
    }
    }


    //门诊充值
    @RequestMapping("/Mztopup")
    @ResponseBody
    public boolean Mztopup(Integer CZvisid,Integer CZmoney,String usefor){
        System.out.println(CZmoney+""+CZvisid+""+usefor);
        ExpenseCalendar ex=new ExpenseCalendar();
        ex.setVisid(CZvisid);
        ex.setPurpose(usefor);
        ex.setMoney(CZmoney);

        int i = visitingcardService.updateBalance(CZvisid, CZmoney);
        if (i > 0) {
          return   expenCalendService.etInsert(ex);//充值记录
        }
        return false;
    }







  //医生段患者查询
  @RequestMapping(value = "/queryAll",method = RequestMethod.GET)
  @ResponseBody
  public String queryAll(HttpSession session){
      String user =(String) session.getAttribute("user");
      Integer did=new Integer(user);
      JSONObject json = new JSONObject();
      List<DiagnoseDB> diagnoseDBS = diagnoseService.queryAll(did);
      Object objData=JSONObject.toJSON(diagnoseDBS);
      json.put("code", 0);
      json.put("msg", "");
      json.put("count",diagnoseDBS.size());
      json.put("data",objData);
      return json.toJSONString();
  }


    //患者段查询
    @RequestMapping(value = "/queryAllHz",method = RequestMethod.GET)
    @ResponseBody
    public String queryAllHz(HttpSession session){
        String user =(String) session.getAttribute("user");
        Integer did=new Integer(user);
        List<Visitingcard> visitingcards = xyService.queryByName(did);
        Integer visid = visitingcards.get(0).getVisid();
        System.out.println(visid+"<==visid");
        JSONObject json = new JSONObject();
        List<DiagnoseDB> diagnoseDBS = diagnoseService.queryHZ(visid);
        Object objData=JSONObject.toJSON(diagnoseDBS);
        json.put("code", 0);
        json.put("msg", "");
        json.put("count",diagnoseDBS.size());
        json.put("data",objData);
        return json.toJSONString();
    }
//    //医生段患者模糊查询
//    @RequestMapping(value = "/queryHZMH",method = RequestMethod.GET)
//    @ResponseBody
//    public String queryHZMH(HttpSession session,String ttime) throws ParseException {
//        String user =(String) session.getAttribute("user");
//        Integer did=new Integer(user);
//        List<Visitingcard> visitingcards = xyService.queryByName(did);
//        Integer visid = visitingcards.get(0).getVisid();
//        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
//           Date  time=format.parse(ttime);
//        JSONObject json = new JSONObject();
//        System.out.println(time+"timetime");
//        List<DiagnoseDB> diagnoseDBS = diagnoseService.queryHZMH(visid,time);
//        Object objData=JSONObject.toJSON(diagnoseDBS);
//        json.put("code", 0);
//        json.put("msg", "");
//        json.put("count",diagnoseDBS.size());
//        json.put("data",objData);
//        System.out.println(json+"<==================>");
//        return json.toJSONString();
//    }




    //医生段患者模糊查询
    @RequestMapping(value = "/selectMH",method = RequestMethod.GET)
    @ResponseBody
    public String selectMH(HttpSession session,String name){
      String user =(String) session.getAttribute("user");
      Integer did=new Integer(user);
        JSONObject json = new JSONObject();
        List<DiagnoseDB> diagnoseDBS = diagnoseService.SelectName(did, name);
        Object objData=JSONObject.toJSON(diagnoseDBS);
        json.put("code", 0);
        json.put("msg", "");
        json.put("count",diagnoseDBS.size());
        json.put("data",objData);
        System.out.println(json+"<==================>");
        return json.toJSONString();
    }








    //藥品展示
    @RequestMapping(value = "/prelist",method = RequestMethod.GET)
    @ResponseBody
    public String prelist(Integer diaid, String time) throws ParseException {
        SimpleDateFormat sdf=new SimpleDateFormat("yyy-MM-dd");
        Date parse = sdf.parse(time);
        JSONObject json = new JSONObject();
        List<PrescriptionDB> presDBS = prescriptionService.preList(diaid,parse);
        Object objData=JSONObject.toJSON(presDBS);
        json.put("code", 0);
        json.put("msg", "");
        json.put("count",presDBS.size());
        json.put("data",objData);
        return json.toJSONString();
    }


//體檢信息展示
    @RequestMapping("/exid")
    @ResponseBody
    public Examine exAll(Integer id){

    return prescriptionService.EXById(id);

    }













}
